<template>
  <div>
    <BasicTable @register="registerTable">
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
      </template>
    </BasicTable>
    <Drawer @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup name="vipcardServer">
import { ActionItem, BasicTable, TableAction } from '@apps/antd-admin/src/components/Table';
import { useDrawer } from '@apps/antd-admin/src/components/drawer';
import Drawer from './drawer.vue';
import { columns, searchFormSchema } from './data';
import { useMessage } from '@apps/antd-admin/src/hooks/web/useMessage';
import { useBaseTable } from '@apps/antd-admin/src/hooks';
import { Api } from './api';

const { createMessage } = useMessage();
const [registerDrawer, { openDrawer }] = useDrawer();
const { prefixCls, tableContext, handleDelete, handleSuccess, handlePost, handleBatchDelete } = useBaseTable({
  designScope: 'vipcardServer-template',
  tableProps: {
    //表格标题
    title: '权益列表',
    //主键字段
    rowKey: 'vipcardServerId',
    //列表接口
    listUrl: Api.page,
    //删除接口
    delUrl: Api.remove,
    //列定义
    columns,
    //查询表单配置
    formConfig: {
      labelWidth: 140,
      labelCol: { span: 7 },
      schemas: searchFormSchema,
    },
    //操作列配置
    actionColumn: {
      width: 120,
      title: '操作',
      dataIndex: 'action',
      slots: { customRender: 'action' },
      fixed: 'right',
    },
  },
});

const [registerTable, { reload, setProps }, { rowSelection, hasSelectedRows }] = tableContext;

/**
 * 行内操作栏
 */
function getTableAction(record): ActionItem[] {
  return [
    {
      label: '编辑',
      icon: 'clarity:note-edit-line',
      onClick: handleEdit.bind(null, record),
    },
  ];
}

/**
 * 下拉操作栏
 */
function getDropDownAction(record): ActionItem[] {
  return [
    {
      label: '启用权益',
      popConfirm: {
        title: '确认启用权益吗？',
        confirm: handleSetState.bind(null, 1, record),
      },
      ifShow: _action => {
        return record.vipcardServerState == 0;
      },
    },
    {
      label: '禁用权益',
      popConfirm: {
        title: '确认禁用权益吗？',
        confirm: handleSetState.bind(null, 0, record),
      },
      ifShow: _action => {
        return record.vipcardServerState == 1;
      },
    },
    {
      label: '删除权益',
      popConfirm: {
        title: '确认删除权益吗？',
        confirm: handleDeleteServer.bind(null, record),
      },
      ifShow: _action => {
        return record.isSet == 2;
      },
    },
  ];
}

function handleEdit(record: Recordable) {
  openDrawer(true, {
    record,
    isUpdate: true,
  });
}

function setSearchInfoProps(vipcardId) {
  console.info('vipcardId', vipcardId);
  setProps({ searchInfo: { vipcardId: vipcardId } });
}

/**
 * 启用禁用
 */
async function handleSetState(vipcardServerState, record) {
  await handlePost(Api.changeVipcardServerState, {
    vipcardServerId: record.vipcardServerId,
    vipcardServerState: vipcardServerState,
  });
  reload();
}
/**
 * 删除权益
 */
async function handleDeleteServer(record) {
  await handlePost(Api.deleteServer, {
    vipcardServerId: record.vipcardServerId,
  });
  reload();
}

defineExpose({
  setSearchInfoProps,
});
</script>
